home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / multitsk.arc / TESTTASK.C < prev    next >
Encoding:
Text File  |  1986-03-05  |  1.8 KB  |  72 lines

  1. int lock[5];
  2. int *comanch;
  3.  
  4. #include "stdio.h"
  5. char *getmem();
  6.  
  7. /*          Test multi-tasking function library
  8.  *
  9.  *   In this test, a main task and three subtasks are created.
  10.  *   Initally, each task identifies itself, then issues yield to
  11.  *   allow the next task to execute.
  12.  *
  13.  *   main then obtains all of the locks, and enters a loop, issuing an
  14.  *   identifying message on each pass.  At cycle 5, 10, 15 and 20,
  15.  *   a lock is released via deq.
  16.  *
  17.  *   Each of the subtasks performs a limited number of iterations of the
  18.  *   following:  enq lock, issue message, then deq lock.
  19.  *
  20.  *   The sequence of displays shows each of the tasks being dispatched
  21.  *   in turn, but only when any needed lock is available.
  22.  *
  23.  *   Note that although parameters may not be passed to the subtasks
  24.  *   as normal arguments, the task-id is given through the
  25.  *   task-related data area passed via attach.
  26.  *
  27.  */
  28.  
  29. main()
  30. {
  31.   static int ids[4] = {1,2,3,4};
  32.   int j;
  33.   int i;
  34.  
  35.   comanch = &ids[0];
  36.   inittask(&ids[0]);
  37.   for (i=1;i<4; i++)
  38.     {   /* get subtask going with 1k stack */
  39.         if (attach(&ids[i],getmem(1024),1024)) subtask();
  40.     }
  41.   printf("\tMain");
  42.   yield();                        /* give other tasks 1 shot each */
  43.   for (i=1;i<5; i++)  enq(&lock[i]);  /* block other tasks */
  44.   for (j=1;j<26;j++)
  45.      {
  46.         if (j==5)  deq(&lock[1]);
  47.         if (j==10) deq(&lock[2]);
  48.         if (j==15) deq(&lock[3]);
  49.         if (j==20) deq(&lock[4]);
  50.         printf("\tMain ");
  51.         yield();
  52.      }
  53.   while (taskcnt() > 1) yield();    /* wait for completion of subtasks */
  54. }
  55.  
  56. subtask()
  57. {
  58.   int i;
  59.   int j;
  60.  
  61.   i = *comanch;
  62.   j = i*10;
  63.   while (j--)
  64.     {
  65.        enq(&lock[i]);
  66.        printf("\t %d",i);
  67.        deq(&lock[i]);
  68.        yield();
  69.     }
  70.   stop();
  71. }
  72.